
import storage from "@/utils/storage.js"
// export const baseUrl = "https://www.teachfree.cn/api"
export const baseUrl = "http://192.168.0.106:8888"
function request({url,method,data={},no_loading}){
	const token = storage.get_token()
	if(!no_loading){
		uni.showLoading({
			title: "加载中..."
		})
	}
	
	data.app_id = "wxc7637458c1925581"
	return new Promise((resolve,reject) => {
		url = baseUrl + url
		uni.request({
			url,
			method,
			data,
			header: {
				"Authorization": "Bearer "+token
			},
			success(res) {
				if(res.data.errno==0){
					if(!no_loading){
						uni.hideLoading()
					}
					resolve(res.data.data)
				}else{
					if(res.data.errno == 401){
						//登录失效
						uni.redirectTo({
							url: "/pages/login/index"
						})
						return
					}
					uni.showToast({
						title: res.data.errmsg,
						icon: "none",
						duration: 2000,
						complete() {
							if(!no_loading){
								setTimeout(() => {
									uni.hideLoading()
								},500)
								
							}
							
							reject(res.data.errmsg)
						}
					})
				}
				
			},
			fail(err) {
				if(!no_loading){
					uni.hideLoading()
				}
				reject(err)
			}
		})
	})
}
export default request